package com.fpt.edu.vn.h2tv.service;

import java.util.ArrayList;
import java.util.List;

import com.fpt.edu.vn.h2tv.common.ElementName;
import com.fpt.edu.vn.h2tv.dao.ITbBookDAO;
import com.fpt.edu.vn.h2tv.dao.TbBookDAO;
import com.fpt.edu.vn.h2tv.dao.entity.TbBook;
import com.fpt.edu.vn.h2tv.dto.Book;
import com.fpt.edu.vn.h2tv.dto.BookResponse;

public class BookService {
	// private int start;
	// private int limit;
	//
	// public BookService(int start,int limit){
	// this.start = start;
	// this.limit = limit;
	// }
	//

	// @Override
	// public Object getResponse(){
	// return getAllBookReponse();
	// }
	/**
	 * 
	 * Get all book Return to client
	 * 
	 * @return BookResponse
	 */
	public static BookResponse getAllBookReponse(int start, int limit,
			int categoryId, String keyWord) {
		ITbBookDAO dao = new TbBookDAO();
		// find all book on database
		List<TbBook> tbBookList = dao.findAllBook(start, limit, categoryId, keyWord);
		BookResponse response = new BookResponse();
		List<Book> bookList = new ArrayList<Book>();
		if (tbBookList != null && tbBookList.size() > 0) {
			for (TbBook book : tbBookList) {
				Book bookDTO = new Book();
				bookDTO.setBookId(book.getBooktid().intValue());
				bookDTO.setBookName(book.getBookname());
				bookDTO.setAuthor(book.getAuthor());
				bookDTO.setPrice(book.getPrice().doubleValue());
				bookDTO.setCategoryId(book.getCategoryid().shortValue());
				bookDTO.setPicture(book.getBookImagel());
				bookList.add(bookDTO);
			}
			response.setStatus(ElementName.SUCCESS);
			Long count = dao.count(categoryId,keyWord);
			response.setCountTotal(count);
		} else {
			response.setStatus(ElementName.NO_DATA);
		}
		response.setBooks(bookList);
		return response;
	}

	public static BookResponse findHotBook() {
		ITbBookDAO dao = new TbBookDAO();
		List<TbBook> tbBookList = dao.findHotBook();
		BookResponse response = new BookResponse();
		List<Book> bookList = new ArrayList<Book>();
		if (tbBookList != null && tbBookList.size() > 0) {
			for (TbBook book : tbBookList) {
				Book bookDTO = new Book();
				bookDTO.setBookId(book.getBooktid().intValue());
				bookDTO.setBookName(book.getBookname());
				bookDTO.setPrice(book.getPrice().doubleValue());
				bookDTO.setPicture(book.getBookImagel());
				bookList.add(bookDTO);
			}
			response.setStatus(ElementName.SUCCESS);
		} else {
			response.setStatus(ElementName.NO_DATA);
		}
		response.setBooks(bookList);
		return response;
	}
	
//	public static BookResponse findBookByKeyword(String keyword) {
//		ITbBookDAO dao = new TbBookDAO();
//		List<TbBook> tbBookList = dao.findBookByKeyword(keyword);
//		BookResponse response = new BookResponse();
//		List<Book> bookList = new ArrayList<Book>();
//		if (tbBookList != null && tbBookList.size() > 0){
//			for (TbBook book : tbBookList){
//				Book bookDTO = new Book();
//				bookDTO.setBookId(book.getBooktid().intValue());
//				bookDTO.setBookName(book.getBookname());
//				bookDTO.setPrice(book.getPrice().doubleValue());
//				bookDTO.setPicture(book.getBookImagel());
//				bookList.add(bookDTO);
//			}
//			response.setStatus(ElementName.SUCCESS);
//			Long count = (long) bookList.size();
//			response.setCountTotal(count);
//		} else {
//			response.setStatus(ElementName.NO_DATA);
//		}
//		response.setBooks(bookList);
//		return response;		
//	}
}
